﻿#region imports

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;

using CondeNet.AssetManager.Data;
using CondeNet.AssetManager.Business;
using CondeNet.AssetManager.Business.Interface;

#endregion

namespace CondeNet.AssetManager.DataProvider.Sql.Mappers
{
    public static class AssetContentRepositoryMapper
    {
        internal static IRepository ToBusinessObject(T_AssetContentRepository entity)
        {
            //IAssetContentRepository obj = new AssetContentRepository(
            //    entity.AssetContentRepository_IDPK,
            //    entity.AssetContentID_FK,
            //    entity.RepositoryID_FK,
            //    entity.StatusID_FK);

            // parent is causing recursion:  if ( entity.T_AssetContent != null ) obj.AssetContent = AssetContentMapper.ToBusinessObject ( entity.T_AssetContent );
            if (entity.T_Repository != null)
                return RepositoryMapper.ToBusinessObject(entity.T_Repository);
            else
            {
                return null;
            }
            //if (entity.T_Status != null) obj.Status = StatusMapper.ToBusinessObject(entity.T_Status);

            //return obj;
        }

        internal static T_AssetContentRepository ToEntity ( int AssetContentId, IRepository v )
        {
            T_AssetContentRepository entity = new T_AssetContentRepository ( );

            //entity.AssetContentRepository_IDPK = v.ID;
            entity.AssetContentID_FK = AssetContentId;
            entity.RepositoryID_FK = v.ID;
            entity.StatusID_FK = 0; // v.StatusID;

            // parent is causing recursion:  if ( v.AssetContent != null ) entity.T_AssetContent = AssetContentMapper.ToEntity ( v.AssetContent );
            entity.T_Repository = RepositoryMapper.ToEntity ( v );
            //if ( v.Status != null ) entity.T_Status = StatusMapper.ToEntity ( v.Status );

            return entity;
        }

        internal static IList<IRepository> ToBusinessObjects(IList<T_AssetContentRepository> dalList)
        {
            return dalList.Select(tmp => ToBusinessObject(tmp)).ToList();
        }

        internal static IList<T_AssetContentRepository>     ToEntities ( int AssetContentId,  IList<IRepository> entityList )
        {
            EntitySet<T_AssetContentRepository> list = new EntitySet<T_AssetContentRepository>();
             entityList.Select ( tmp => ToEntity ( AssetContentId, tmp ) ).ToList ( ).ForEach(list.Add);
            return list;
        }
    }
}
